package com.shuyuntu.proposal.dao.hib;

import com.shuyuntu.proposal.dao.ISecondMotionDao;
import com.shuyuntu.proposal.dao.base.BaseDaoHib;
import com.shuyuntu.proposal.entity.SecondMotion;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 * 附议对象的数据访问对象的Hibernate实现
 *
 * @author limeng
 * Created by limeng on 16-4-17.
 */
@Repository("secondMotionDao")
public class SecondMotionDaoHib extends BaseDaoHib<SecondMotion, Integer> implements ISecondMotionDao {
    @Override
    public Integer insert(SecondMotion secondMotion) {
        Session session = sessionFactory.getCurrentSession();

        Criterion cr1 = Restrictions.eq("author.id", secondMotion.getAuthor().getId());
        Criterion cr2 = Restrictions.eq("targetProposal.id", secondMotion.getTargetProposal().getId());
        long count = this.queryCount(cr1, cr2);

        if (count > 0) {
            return null;
        }

        return (Integer)session.save(secondMotion);
    }
}
